layui.use(['jquery','table','layer','laydate'],function () {
    var $ = layui.jquery;
    var table = layui.table;
    var layer = layui.layer;

    /*获取轮播图列表*/
    getAllBanner("","");
    function getAllBanner(imgType,display) {
        table.render({
            elem:"#root",
            url:"/newBanner/getAllBanner?imgType="+imgType+"&display="+display,
            defaultToolbar: ['','',''],
            cellHeight:57,
            page:true,
            limit:4,
            limits:[4,8,12,16],
            size: 'lg',
            cols:[[
                {type:'checkbox',align:"center"},
                {field:'carouselId',title:'轮播图编号',align:"center"},
                {title:'轮播图图片',align:"center",height:"57px",templet:function (data) {
                        return "<img src=http://"+data.imgAddress+">";
                    }},
                {field:'bannerName',title:'图片名称',align:"center"},
                {field:'trueName',title:'创建人',align:"center"},
                {field:'imgType',title:'图片类型',align:"center",templet:function (data) {
                    if(data.imgType=="2"){
                        return "商品";
                    }else{
                        return "视频";
                    }
                    }},
                {field:'remarks',title:'备注',align:"center"},
                {field:'updateDate',title:'更新时间',align:"center",templet:function (data) {
                    if(data.updateDate){
                        return "<div>"+data.updateDate+"</div>";
                    }else{
                        return "<div>暂未更新</div>";
                    }
                }},
                {title:'操作',align:"center",templet:function (data) {
                        return "<div>"
                            +"<a href='javascript:chakan("+data.carouselId+");' style='color: #2F4056;margin-right: 10px;'>查看</a>"
                            +"<a href='javascript:bianji("+data.carouselId+");' style='color: #2F4056;margin-right: 10px;'>编辑</a>"
                            +"<a href='javascript:shanchu("+data.carouselId+");' style='color: #2F4056;margin-right: 10px;'>删除</a>"
                            +"</div>";
                    }}
            ]]
        })
    };
    /*搜素*/
    $("#sousuo").click(function () {
        var imgType = $("#imgType").val();
        var display = $("#display").val();
        getAllBanner(imgType,display);
    });
    /*重置*/
    $("#reset").click(function () {
      location.reload();
    });
    /*删除*/
    shanchu = function shanchu(id) {
        layer.confirm('是否确定删除此条数据', {
            icon: 3, title: '提示'
        },function(){
            $.ajax({
                type:"post",
                dataType:"json",
                data:{"id":id},
                url:"/newBanner/delBanner",
                success:function (data) {
                    alert("删除成功");
                    location.reload();
                },error:function (data) {
                    alert("删除失败");
                    location.reload();
                }
            })
        })
    }
    /*查看*/
    chakan = function chakan(id){
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id},
            url:"/newBanner/getOnlyBanner",
            success:function (data) {
                showBanner(data.data);
            }
        })
    };
    function showBanner(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"查看轮播图信息",
            content:"/newbanner/banner-list-see.html",
            area:['500px','650px'],
            btn:["关闭"],
            yes:function (index) {
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find("#bannerName").val(data.bannerName);
                var imgType ="";
                if(data.imgType=="2"){
                    imgType= "商品";
                }else{
                    imgType= "视频";
                }
                obj.find("#imgType").val(imgType);
                obj.find("#startDate").val(data.startDate);
                obj.find("#expireDate").val(data.expireDate);
                var display="";
                if(data.display=="2"){
                    display = "显示";
                }else{
                    display = "不显示";
                }
                obj.find("#display").val(display);
                obj.find("#imgAddress").attr('src',"http://"+data.imgAddress);
                obj.find("#imgAddress").attr('style','width: 80px;height:80px;border:2px solid black');
                obj.find("#remarks").val(data.remarks);
            }
        })
    };

    /*编辑*/
    bianji = function bianji(id) {
        $.ajax({
            type:"post",
            dataType:"json",
            data:{"id":id},
            url:"/newBanner/getOnlyBanner",
            success:function (data) {
                editBanner(data.data);
            }
        })
    };
    function editBanner(data){
        var obj;
        layer.open({
            type:2,
            shode:0,
            maxmin:true,
            title:"编辑轮播图信息",
            content:"/newbanner/banner-list-edit.html",
            area:['520px','650px'],
            btn:["确定","取消"],
            yes:function (index) {
                var updateDate = new Date();
                updateDate = getTime(updateDate);
                var json = {
                    bannerName:obj.find('#bannerName').val(),
                    imgType:obj.find('#imgType').val(),
                    startDatee:obj.find('#startDate').val(),
                    expireDatee:obj.find('#expireDate').val(),
                    display:obj.find('#display').val(),
                    imgAddress:obj.find('#imgAddress').attr('src'),
                    remarks:obj.find('#remarks').val(),
                    updateDatee:updateDate,
                    carouselId:data.carouselId
                }
                $.ajax({
                    type:"post",
                    dataType:"json",
                    data:json,
                    url:"/newBanner/updateBanner",
                    success:function (data) {
                        if(data){
                            alert("编辑成功");
                        }
                        location.reload();
                    },error:function () {
                        alert("编辑失败");
                    }
                })
            },btn2:function (index) {
                layer.close(index);
            },success:function (layero,index) {
                obj = layero.find('iframe').contents();
                obj.find("#bannerName").val(data.bannerName);
                var imgType ="";
                if(data.imgType=="2"){
                    imgType= "商品";
                }else{
                    imgType= "视频";
                }
                obj.find("#imgType").val(imgType);
                obj.find("#startDate").val(data.startDate);
                obj.find("#expireDate").val(data.expireDate);
                var display="";
                if(data.display=="2"){
                    display = "显示";
                }else if(data.display=="3"){
                    display = "不显示";
                }
                obj.find("#display").val(display);
                obj.find("#imgAddress").attr('src',"http://"+data.imgAddress);
                obj.find("#imgAddress").attr('style','width: 80px;height:80px;border:2px solid black');
                obj.find("#remarks").val(data.remarks);
                selectLay(obj,data.display,display,"#display");
            }
        })
    };
    /*下拉框*/
    function selectLay(obj, layValue, value, select) {
        obj.find(select).children().each(function () {
            if ($(this).val() == layValue) {
                var status = 'dd[lay-value=' + layValue + ']';
                obj.find(select).siblings("div.layui-form-select").find('dl').find(status).click();
                $(this).parent().append('<option value="' + layValue + '" selected>' + value + '</option>');
            }
        })
    }

    /*获取一个当前时间*/
    function getTime(date){
        var year = date.getFullYear(); //获取完整的年份(4位,1970-????)
        var month = date.getMonth()+1; //获取当前月份(0-11,0代表1月)
        var today = date.getDate(); //获取当前日(1-31)
        var hour = date.getHours(); //获取当前小时数(0-23)
        var minute = date.getMinutes(); //获取当前分钟数(0-59)
        var second = date.getSeconds(); //获取当前秒数(0-59)
        var nowTime;
        nowTime = year+'-'+fillZero(month)+'-'+fillZero(today)+' '+fillZero(hour)+':'+fillZero(minute)+':'+fillZero(second);
        return nowTime;
    };
    /*时间的补零操作*/
    function fillZero(str){
        var realNum;
        if(str<10){
            realNum	= '0'+str;
        }else{
            realNum	= str;
        }
        return realNum;
    };

    /*刷新*/
    function Refresh() {
        location.reload();
    }

    /*返回*/
    function goBack() {
        //通过页面查找iframe
        parent.$('#Jump').val('首页');
        parent.getQuick();
        return;
    }
});